﻿using System;
using System.Linq;
using System.Collections.Generic;

using StackExchange.Redis;
using TrinityAPI.Base;
using System.Data.Common;

namespace TrinityAPI.Cache.Redis
{
    public class CacheManager: IDisposable
    {
        private int _references;
        private DateTime _lastVisitTime;
        private ConnectionMultiplexer _redisConnection;
        public ConnectionMultiplexer RedisConnection
        {
            get
            {
                _lastVisitTime = DateTime.Now;
                return _redisConnection;
            }
        }

        public DateTime LastVisitTime
        {
            get
            {
                return _lastVisitTime;
            }
        }

        public int References
        {
            get
            {
                return _references;
            }
            set
            {
                _references = value < 0 ? 0 : value;
            }
        }

        public CacheManager(ConnectionMultiplexer connection)
        {
            if (connection == null)
                throw new ArgumentNullException(nameof(connection));
            _lastVisitTime = DateTime.Now;
            _redisConnection = connection;
        }

        public void Dispose()
        {
            _references = 0;
            _redisConnection.Dispose();
        }
    }
}
